import Vue from 'vue'
import Router from 'vue-router'
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

Vue.use(Router)

import Layout from '@/layout'

// 动态路由表, 项目中不用的用户可以访问不同的功能
export const asyncRoutes = [
  // 先空着这里, 后面来补充
  departmentsRouter, //组织架构
  settingRouter, //角色设置
  employeesRouter, // 员工
  permissionRouter, // 权限管理
  approvalsRouter, // 审批
  attendancesRouter, // 考勤
  salarysRouter, // 工资
  socialRouter // 社保
]
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/excel',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/excel')
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    // 注意 : 如果把动态路由规则对象的数组, 放到后面, 静态路由的path : * 会在中间,所以如果你在动态路由页面退出登录,重新进入的时候,暗战数组里匹配规则, 会先匹配path : 星号, 导致404,可以考虑把动态路由规则数组放到前面
    routes: [...constantRoutes, ...asyncRoutes]
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
